package lrh.hubu.firstproject;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private LinearLayout l1;
    private LinearLayout l2;
    private LinearLayout l3;
    private LinearLayout l4;

    private ImageButton i1;
    private ImageButton i2;
    private ImageButton i3;
    private ImageButton i4;

    private Fragment t1 = new LOLFragment();
    private Fragment t2 = new gameFragment();
    private Fragment t3 = new talkFragment();
    private Fragment t4 = new privateFragment();

    private FragmentManager fragmentManager ;

//    RecyclerView rcvGrid = findViewById(R.id.recyclerview1);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

//        rcvGrid = findViewById(R.id.recyclerview1);

        initView();
        initFragment();
        initEvent();
        selectFragment(0);
    }

    private void initFragment(){
        //版本不对。要换
        fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.zhuomian,t1);
        transaction.add(R.id.zhuomian,t2);
        transaction.add(R.id.zhuomian,t3);
        transaction.add(R.id.zhuomian,t4);
        transaction.commit();
    }

    private void initView(){
        l1 = (LinearLayout)findViewById(R.id.lol);
        l2 = (LinearLayout)findViewById(R.id.game);
        l3 = (LinearLayout)findViewById(R.id.talk);
        l4 = (LinearLayout)findViewById(R.id.pri);

        i1 = (ImageButton)findViewById(R.id.imageButton1);
        i2 = (ImageButton)findViewById(R.id.imageButton2);
        i3 = (ImageButton)findViewById(R.id.imageButton3);
        i4 = (ImageButton)findViewById(R.id.imageButton4);
    }

    private void initEvent(){
        i1.setOnClickListener(this);
        i2.setOnClickListener(this);
        i3.setOnClickListener(this);
        i4.setOnClickListener(this);
    }

    private void selectFragment(int i){
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        hideFragment(transaction);
        switch (i){
            case 0:
                transaction.show(t1);
                i1.setImageResource(R.drawable.lol2);
                break;
            case 1:
                transaction.show(t2);
                i2.setImageResource(R.drawable.game2);
                break;
            case 2:
                transaction.show(t3);
                i3.setImageResource(R.drawable.talk2);
                break;
            case 3:
                transaction.show(t4);
                i4.setImageResource(R.drawable.private2);
                break;
            default:
                break;
        }
        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction){
        transaction.hide(t1);
        transaction.hide(t2);
        transaction.hide(t3);
        transaction.hide(t4);
    }

    @Override
    public void onClick(View view) {
        reset();
        switch (view.getId()){
            case R.id.imageButton1:
                selectFragment(0);
                break;
            case R.id.imageButton2:
                selectFragment(1);
                break;
            case R.id.imageButton3:
                selectFragment(2);
                break;
            case R.id.imageButton4:
                selectFragment(3);
                break;
            default:
                break;
        }
    }

    public void reset(){
        i1.setImageResource(R.drawable.lol);
        i2.setImageResource(R.drawable.game);
        i3.setImageResource(R.drawable.talk);
        i4.setImageResource(R.drawable.pri);
    }
}